package com.hnu.highlynewapp.authentication;

import com.hnu.highlynewapp.entity.ContactEntity;
import com.hnu.highlynewapp.entity.DepartmentInfoEntity;
import com.hnu.highlynewapp.enums.StatusEnum;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.service.ContactService;
import com.hnu.highlynewapp.service.DepartmentInfoService;
import com.hnu.highlynewapp.utils.R;
import com.hnu.highlynewapp.vo.DepartmentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api("科技主管部门认证api")
@RestController
@RequestMapping("/departmentinfoauthentication")
public class DepartmentInfoAuthenticationController {
    @Autowired
    DepartmentInfoService departmentInfoService;
    @Autowired
    ContactService contactService;
    @ApiOperation("保存科技主管部认证信息")
    @PostMapping(value = "/save")
    //@RequiresPermissions("highlynewapp:businessinfo:save")
    public R save(@Validated @RequestBody DepartmentInfoVo departmentInfoVo, BindingResult result) {
        Map<String, String> map = new HashMap<>();

        if (result.hasErrors()) {
            //获取效验错误结果
            result.getFieldErrors().forEach((item) -> {
                //获取到错误提示
                String message = item.getDefaultMessage();
                //获取错误的属性的名字
                String field = item.getField();
                map.put(field, message);
            });
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data", map);
        } else {
            departmentInfoService.auth(departmentInfoVo);

            return R.ok();
        }
    }
    @ApiOperation("显示所有还未认证的科技主管部认证信息")
    @PostMapping(value="/nocheck")
    public R nocheck()
    {
        List<DepartmentInfoEntity> departmentInfoEntityList=departmentInfoService.getNoCheckList();

        List<DepartmentInfoVo> departmentInfoVoList= new ArrayList<DepartmentInfoVo>() {//这个大括号 就相当于我们  new 接口
            {//这个大括号 就是 构造代码块 会在构造函数前 调用


                int i;

                for(i=0;i<departmentInfoEntityList.size();i++)
                {
                    DepartmentInfoVo departmentInfoVo=new DepartmentInfoVo();
                    ContactEntity contactEntity=contactService.getById(departmentInfoEntityList.get(i).getContactId());
                    System.out.println(contactEntity);
                    BeanUtils.copyProperties(departmentInfoEntityList.get(i),departmentInfoVo);

                    departmentInfoVo.setContactAddress(contactEntity.getAddress());
                    departmentInfoVo.setContactEmail(contactEntity.getEmail());
                    departmentInfoVo.setContactName(contactEntity.getName());

                    departmentInfoVo.setContactPhone(contactEntity.getPhone());
                    departmentInfoVo.setContactPosition(contactEntity.getPosition());
                    add(departmentInfoVo);
                }

            }
        };
        return R.ok().put("data",departmentInfoVoList);
    }
    @ApiOperation("通过departmentId来通过认证")
    @PostMapping("/passcheck/{departmentId}")
    public R passcheck(@PathVariable("departmentId") Long departmentId)
    {
        departmentInfoService.passCheck(departmentId);

        return R.ok();
    }
    @ApiOperation("通过departmentId来拒绝通过认证")
    @PostMapping("/failcheck/{departmentId}")
    public R failcheck(@PathVariable("departmentId") Long departmentId)
    {
        departmentInfoService.failCheck(departmentId);

        return R.ok();
    }
}

